package com.xlh.util;

import com.xlh.exception.CmdException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * <p>
 * Created by lx on 2019/1/3.
 */
public class CmdUtil {

    private static Logger logger = LoggerFactory.getLogger(CmdUtil.class);

    public static void exec(String cmd) {
        try {
            String[] commands = new String[]{"/bin/bash", "-c", cmd};
            Runtime runtime = Runtime.getRuntime();
            Process process = runtime.exec(commands);

            if (process.waitFor() != 0) {
                throw new CmdException(cmd + "命令执行失败");
            }
        } catch (Exception e) {
            if(e instanceof CmdException){
                throw new CmdException(e);
            }
            logger.error(e.getMessage(), e);
            throw new CmdException(e);
        }
    }

}
